#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main()
{
	int fd[2] = {0};
	int ret = 0;
	pid_t pid = 0;

	ret = pipe(fd);
	if(ret == -1)
	{
		perror("pipe");
		exit(-1);
	}

	pid = fork();
	if(pid == 0)
	{
		char buf[50] = {0};
		close(fd[1]);
		ret = read(fd[0],buf,49);
		if(ret == -1)
		{
			perror("read");
			exit(-1);
		}
		printf("buf is %s\n",buf);
		close(fd[0]);

		exit(0);
	}else if(pid < 0)
	{
		printf("fork error\n");
	}

	close(fd[0]);

	char buf[50] = {0};
	printf("please input a string:\n");
	scanf("%s",buf);

	ret = write(fd[1],buf,strlen(buf));
	if(ret == -1)
	{
		perror("write");
		exit(-1);
	}

	wait(NULL);
	close(fd[1]);

	return 0;

}
